package SSF.OS.NetFlow;

import SSF.OS.*;
import java.io.*;

/**
 * A shrinkable-hash table of IpNetFlows. 
 * When colisions happen, use link lists to extends the entries.
 */
public class IpFlowTable extends ShrinkableHashMap
{
    /** protocol interested in */
    public byte protocol_type;

    /** default constructor */
    public IpFlowTable() {
	super();
	System.err.println("protocol type not specified in IpFlowTable");
    }

    /** 
     * constructor specify the protocol interested
     * @param protcol protocol interested in
     */
    public IpFlowTable(byte protocol, int initSize)
    {
	super(initSize);
	protocol_type = protocol;
    }

    /** construct with a given flow */
    public IpFlowTable(Object key, IpNetFlow flow)
    {
	super();
	put(key,flow);
    }


    /**
     * remove the given netflow with given key from table
     * @param key the key of this flow
     * @param flow the flow to be removed
     */
    public void removeFlow(Object key, IpNetFlow flow) {
	//-- check whether parameter is valid
	if (flow == null) {
	    //-- System.err.println("Warning: attempt to remove null flow in Ip            //-- FlowTable");
	    return;
	}

	//-- find the list associated with this key
        IpNetFlow flowList = (IpNetFlow)get(key);

	if (flowList == null) {
	    System.err.println("Error in IpFlowTable.removeFlow, attempt to remove entry that does not exist.");
	    return;
	}

	IpNetFlow oldList = flowList;

	flowList = (IpNetFlow)flowList.removeFlow(flow);

	if (flowList == null) {
	    //-- the only element of that entry is gone
	    super.remove(key);
	}
	else if (flowList != oldList) {
	    //-- the first element is removed, a re-insert is required
	    put(key, flowList);
	}
    }
}













